home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / U-Z / UltraShell Demo 1.1.sit / UltraShell™ Demo V1.1 / ExampleExternalCommand / ucommand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-26  |  4.5 KB  |  273 lines  |  [TEXT/KAHL]

  1. /* Version C */
  2.  
  3. /*===========================================================\
  4. |                     UltraShell (tm)                        |
  5. |                                                            |
  6. |                       ucommand.c                           |
  7. |     This module permits external commands to obtain        |
  8. |     command line arguments and to access UltraShell        |
  9. |     files for standard input, output, and error.           |
  10. |                                                            |
  11. |           Copyright (C) 1994 by Zack T. Smith              |
  12. |                                                            |
  13. |     Permission is granted to freely distribute this        |
  14. |    file, so long as the data contained herein is not       |
  15. |                  altered in any way.                       |
  16. \===========================================================*/
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <Files.h>
  22. #include "ucommand.h"
  23.  
  24.  
  25. FILE *shell_stdin;
  26. FILE *shell_stdout;
  27. FILE *shell_stderr;
  28.  
  29.  
  30. extern errno;
  31.  
  32.  
  33. static bcopy (char *dest, char *src, unsigned long count)
  34. {
  35.     while (count--)
  36.         *dest++ = *src++;
  37. }
  38.  
  39.  
  40. static char *strdup (char *str)
  41. {
  42.     char *new;
  43.     
  44.     new = (char*) malloc (strlen (str) + 1);
  45.  
  46.     strcpy (new, str);
  47.  
  48.     return new;
  49. }
  50.  
  51.  
  52. static void
  53. p2c (char *dest, char *src)
  54. {
  55.     int len = *src;
  56.     
  57.     bcopy (dest, src+1, len);
  58.     dest[len] = 0;
  59. }
  60.  
  61.  
  62. static void
  63. c2p (unsigned char *dest, char *src)
  64. {
  65.     int len =  strlen (src);
  66.     
  67.     if (len>255)
  68.     {
  69.         dest[0] = 0;
  70.         return;
  71.     }
  72.     dest[0] = len;
  73.     bcopy ((char*)(dest+1), src, len);
  74. }
  75.  
  76.  
  77. static remove_newlines (char *str)
  78. {
  79.     char *tmp;
  80.     
  81.     tmp = strchr (str, '\n');
  82.     if (tmp) *tmp = 0;
  83.     tmp = strchr (str, '\r');
  84.     if (tmp) *tmp = 0;
  85. }
  86.  
  87.  
  88. static int
  89. get_tempdir_path (char *return_path)
  90. {
  91.     FILE *f;
  92.     char path [MAX_PATH_LEN];
  93.     short vol;
  94.     Str31 vol_name;
  95.     long system_folder;
  96.     short oserr;
  97.     
  98.     if (GetVol (vol_name, &vol))
  99.     {
  100.         /* printf ("cannot get volume information\n"); */
  101.         return CANNOT_FIND_TEMP_DIR;
  102.     }
  103.     
  104.     p2c (path, (char*) vol_name);
  105.     strcat (path, ":System Folder:UltraShellTempDirLocation");
  106.     
  107.     f = fopen (path, "r");
  108.     if (!f)
  109.     {
  110.         /* printf ("can't open %s\n", path); */
  111.         return CANNOT_FIND_TEMP_DIR;
  112.     }
  113.     
  114.     fgets (return_path, MAX_PATH_LEN, f);
  115.     fclose(f);
  116.     
  117.     remove_newlines (return_path);
  118.     
  119.     return SUCCESS;
  120. }
  121.  
  122.  
  123. static int
  124. setup_stdio (void)
  125. {
  126.     FILE *f;
  127.     char path [MAX_PATH_LEN];
  128.     char tmpdir [MAX_PATH_LEN];
  129.  
  130.     shell_stdout = NULL;
  131.     shell_stdin = NULL;
  132.     shell_stderr = NULL;
  133.     
  134.     if (get_tempdir_path (tmpdir))
  135.         return 0;
  136.  
  137.     strcpy (path, tmpdir);
  138.     strcat (path, "stdin");
  139.  
  140.     f = fopen (path, "r");
  141.     if (f)
  142.     {
  143.         shell_stdin = f;
  144.     }
  145.  
  146.     strcpy (path, tmpdir);
  147.     strcat (path, "stdout");
  148.  
  149.     f = fopen (path, "w");
  150.     if (f)
  151.         shell_stdout = f;
  152.  
  153.     strcpy (path, tmpdir);
  154.     strcat (path, "stderr");
  155.  
  156.     f = fopen (path, "w");
  157.     if (f)
  158.         shell_stderr = f;
  159.         
  160.     return 1;
  161. }
  162.  
  163.  
  164. void
  165. shell_exit (short return_value)
  166. {
  167.     FILE *f;
  168.     char path [MAX_PATH_LEN];
  169.     long dummy;
  170.  
  171.     if (shell_stdin)     fclose (shell_stdin);
  172.     if (shell_stdout)     fclose (shell_stdout);
  173.     if (shell_stderr)     fclose (shell_stderr);
  174.     
  175.     if (get_tempdir_path (path))
  176.         exit (1);
  177.  
  178.     strcat (path, "return_value");
  179.  
  180.     f = fopen (path, "w");
  181.     if (f)
  182.     {
  183.         fprintf (f, "%d\n", return_value);
  184.         fclose (f);
  185.     }
  186.  
  187.     exit (0);
  188. }
  189.  
  190.  
  191. long
  192. ucommand (char **argv, unsigned long max_args)
  193. {
  194.     FILE *argfile;
  195.     char line [500];
  196.     Str255 cwd;
  197.     unsigned long argc;
  198.     unsigned long i;
  199.     char args_file_path [MAX_PATH_LEN];
  200.  
  201.     if (get_tempdir_path (args_file_path))
  202.     {
  203.         printf ("Can't get the temp dir path\n");
  204.         return 0;
  205.     }
  206.     
  207.     strcat (args_file_path, "args");
  208.  
  209.     argfile = fopen (args_file_path, "r");
  210.     if (!argfile)
  211.     {
  212.         printf ("Can't open args file %s\n", args_file_path);
  213.         return 0;
  214.     }
  215.     
  216.     if (!fgets (line, 499, argfile))
  217.     {
  218.         printf ("Can't read the working directory path from the args file.\n");
  219.         return 0;
  220.     }
  221.     
  222.     remove_newlines (line);
  223.     
  224.     if (strlen(line) > 255)
  225.     {
  226.         printf ("The working directory path string is too long (over 255 chars).\n");
  227.         return 0;
  228.     }
  229.     
  230.     c2p (cwd, line);
  231.     
  232.     if (HSetVol (cwd, 0, 0L))
  233.     {
  234.         printf ("Cannot change to the working directory specified by the shell.\n");
  235.         return 0;
  236.     }
  237.  
  238.     if (!fgets (line, 499, argfile))
  239.     {
  240.         printf ("Can't read the argc line from args file.\n");
  241.         return 0;
  242.     }
  243.  
  244.     if (1 != sscanf (line, "%lu", &argc))
  245.     {
  246.         printf ("Can't read argc value\n");
  247.         return 0;
  248.     }
  249.     
  250.     if (argc > max_args)
  251.         argc = max_args;
  252.  
  253.     for (i = 0; i < argc; i++)
  254.     {
  255.         if (!fgets (line, 499, argfile))
  256.             return 0;
  257.         
  258.         remove_newlines (line);
  259.  
  260.         argv [i] = strdup (line);
  261.     }
  262.     
  263.     fclose (argfile);
  264.     
  265.     if (!setup_stdio ())
  266.         ;
  267.     
  268.     return argc;
  269. }
  270.  
  271.  
  272.  
  273.